package com.nuosu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Environment;
import android.util.Log;

import com.kincai.libjpeg.ImageUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by SensYang on 2017/05/31 18:56
 */

public class CreatImage {
    private static String nuosuFonts[] = {"FZYWBT", "FZYWFST", "FZYWHT", "FZYWST", "FZYWSXT", "FZYWXHT", "FZYWYTT", "FZYWZYT", "MPYWSXT"};
    private static String imagePath = Environment.getExternalStorageDirectory() + File.separator + "nuosu" + File.separator + "images" + File.separator;
    private static Context context;

    public static void creatImages(Context context) {
        CreatImage.context = context;
        new Thread() {
            @Override
            public void run() {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < nuosuFonts.length; i++) {
                    sb.append(createImage(i));
                }
                saveBoxInfo(sb.toString(), "nuosu.font.exp0");
            }
        }.start();
    }

    private static final int textSize = 48;
    private static final int vertical_space = textSize / 6;
    private static final int text_padding_left = textSize / 8;
    private static final int text_padding_top = textSize / 8;

    private static final int columnCount = 43;
    private static final int lineCount = (1165 / columnCount + 1);
    private static final int bitmap_width = 2 * vertical_space + columnCount * (textSize + text_padding_left) - text_padding_left;
    private static final int bitmap_height = 2 * vertical_space + lineCount * (textSize + vertical_space + text_padding_top) - text_padding_top - vertical_space;

    private static String createImage(int fontNameAt) {
        String fontName = nuosuFonts[fontNameAt];
        Paint textBackPaint = new Paint();
        textBackPaint.setAntiAlias(true);
        textBackPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        Paint textPaint = new Paint();
        textPaint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/" + fontName + ".ttf"));
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(Paint.Align.LEFT);
        textPaint.setColor(Color.BLACK);
        textPaint.setAntiAlias(true);

        Bitmap bitmap = Bitmap.createBitmap(bitmap_width, bitmap_height + vertical_space, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        StringBuilder sb = new StringBuilder();
        int c = 40960;
        for (int i = 0; i < lineCount; i++) {
            for (int j = 0; j < columnCount; j++) {
                int X = j * (textSize + text_padding_left) + vertical_space;
                int Y = i * (textSize + vertical_space + text_padding_top) + vertical_space;
                int width = textSize;
                int height = textSize + vertical_space;
                int text_X = X;
                int text_Y = Y + textSize + text_padding_top;
                canvas.drawText(String.valueOf((char) c), text_X, text_Y, textPaint);
                //char  X   Y   bitmap_width   bitmap_height  page
                //s     143 15  156     31      0
                //s 143 15 156 31 0
                Rect rect = new Rect(X, Y, X + width, Y + height);
                checkRect(bitmap, rect);
                rect.set(rect.left, rect.top - text_padding_top - 2, rect.right, rect.bottom - text_padding_top - 1);
                sb.append("\n");
                sb.append((char) c);
                sb.append(" ");
                sb.append(rect.left);
                sb.append(" ");
                sb.append(bitmap_height - rect.top - rect.height());
                sb.append(" ");
                sb.append(rect.left + rect.width());
                sb.append(" ");
                sb.append(bitmap_height - rect.top);
                sb.append(" ");
                sb.append(fontNameAt);
                //                                textBackPaint.setColor(Color.parseColor("#88AAFFDA"));
                //                                canvas.drawRect(X, Y, X + bitmap_width, Y + bitmap_height,textBackPaint);
                //                textBackPaint.setColor(Color.parseColor("#88888888"));
                //                canvas.drawRect(rect, textBackPaint);
                c++;
                if (c > 42124) {
                    break;
                }
            }
            if (c > 42124) {
                break;
            }
        }
        saveMyBitmap(bitmap, fontName);
        ImageUtils.saveBitmap(bitmap, 100, imagePath + fontName + "page_" + fontNameAt + ".jpg", true);
        bitmap.recycle();
        Log.i("create--->", "create image success:" + fontName);
        return sb.toString();
    }

    private static void checkRect(Bitmap bitmap, Rect rect) {
        checkLeft(bitmap, rect);
        checkTop(bitmap, rect);
        checkRight(bitmap, rect);
        checkBottom(bitmap, rect);
    }

    private static void checkLeft(Bitmap bitmap, Rect rect) {
        boolean hasBlack = false;
        for (int i = 0; i < rect.height(); i++) {
            int color = bitmap.getPixel(rect.left, rect.top + i);
            if (color != Color.WHITE) {
                hasBlack = true;
                for (int j = 0; j < rect.height(); j++) {
                    color = bitmap.getPixel(rect.left - 1, rect.top + j);
                    if (color != Color.WHITE) {
                        rect.set(rect.left - 1, rect.top, rect.right, rect.bottom);
                        checkLeft(bitmap, rect);
                        break;
                    }
                }
                break;
            }
        }
        if (!hasBlack) {
            rect.set(rect.left + 1, rect.top, rect.right, rect.bottom);
            checkLeft(bitmap, rect);
        }
    }

    private static void checkTop(Bitmap bitmap, Rect rect) {
        boolean hasBlack = false;
        for (int i = 0; i < rect.width(); i++) {
            int color = bitmap.getPixel(rect.left + i, rect.top);
            if (color != Color.WHITE) {
                hasBlack = true;
                for (int j = 0; j < rect.width(); j++) {
                    color = bitmap.getPixel(rect.left + j, rect.top - 1);
                    if (color != Color.WHITE) {
                        rect.set(rect.left, rect.top - 1, rect.right, rect.bottom);
                        checkTop(bitmap, rect);
                        break;
                    }
                }
                break;
            }
        }
        if (!hasBlack) {
            rect.set(rect.left, rect.top + 1, rect.right, rect.bottom);
            checkTop(bitmap, rect);
        }
    }

    private static void checkRight(Bitmap bitmap, Rect rect) {
        boolean hasBlack = false;
        for (int i = 0; i < rect.height(); i++) {
            int color = bitmap.getPixel(rect.right, rect.top + i);
            if (color != Color.WHITE) {
                hasBlack = true;
                for (int j = 0; j < rect.height(); j++) {
                    color = bitmap.getPixel(rect.right + 1, rect.top + j);
                    if (color != Color.WHITE) {
                        rect.set(rect.left, rect.top, rect.right + 1, rect.bottom);
                        checkRight(bitmap, rect);
                        break;
                    }
                }
                break;
            }
        }
        if (!hasBlack) {
            rect.set(rect.left, rect.top, rect.right - 1, rect.bottom);
            checkRight(bitmap, rect);
        }
    }

    private static void checkBottom(Bitmap bitmap, Rect rect) {
        boolean hasBlack = false;
        for (int i = 0; i < rect.width(); i++) {
            int color = bitmap.getPixel(rect.left + i, rect.bottom);
            if (color != Color.WHITE) {
                hasBlack = true;
                for (int j = 0; j < rect.width(); j++) {
                    color = bitmap.getPixel(rect.left + j, rect.bottom + 1);
                    if (color != Color.WHITE) {
                        rect.set(rect.left, rect.top, rect.right, rect.bottom + 1);
                        checkBottom(bitmap, rect);
                        break;
                    }
                }
                break;
            }
        }
        if (!hasBlack) {
            rect.set(rect.left, rect.top, rect.right, rect.bottom - 1);
            checkBottom(bitmap, rect);
        }
    }


    public static void saveMyBitmap(Bitmap bitmap, String bitName) {
        try {
            File dir = new File(imagePath);
            if (!dir.exists() || !dir.isDirectory()) {
                if (!dir.mkdirs()) return;
            }
            File file = new File(imagePath + bitName + ".jpg");
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    return;
                }
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void saveBoxInfo(String text, String bitName) {
        try {
            File dir = new File(imagePath);
            if (!dir.exists() || !dir.isDirectory()) {
                if (!dir.mkdirs()) return;
            }
            File file = new File(imagePath + bitName + ".box");
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    return;
                }
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(text.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
